/**
*      author: Y_Yao
*      created: 2025-04-06 12:48:52
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 1000 + 7;
int h,w,sx,sy,ex,ey;
char a[N][N];
int ans[N][N];
bool vis[N][N];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
deque<int> q;

void bfs()
{
    ans[sx][sy] = 0;
    q.push_back(sx * w + sy);
    bool wall = false;
    while(!q.empty())
    {
        int t = q.front();
        q.pop_front();
        int x = t / w,y = t % w;
        if(t == (ex * w + ey))
        {
            cout << ans[x][y] << endl;
            return;
        }
        for(int k = 0;k < 4;k++)
        {
            wall = false;
            for(int j = 1;j <= 2;j++)
            {
                int nx = x + dx[k] * j;
                int ny = y + dy[k] * j;
                if(nx < 0 || nx >= h || ny < 0 || ny >= w) break;
                if(a[nx][ny] == '#')
                {
                    wall = true;
                }
                if(wall)
                {
                    if(ans[nx][ny] > ans[x][y] + 1)
                    {
                        ans[nx][ny] = ans[x][y] + 1;
                        q.push_back(nx * w + ny);
                    }
                }
                else
                {
                    if(j == 1)
                    {
                        if(ans[nx][ny] > ans[x][y])
                        {
                            ans[nx][ny] = ans[x][y];
                            q.push_front(nx * w + ny);//优先处理点
                        }
                    }
                }
            }
        }

    }
}

void solve()
{
    cin >> h >> w;
    for(int i = 0;i < h;i++)
    {
        for(int j = 0;j < w;j++)
        {
            cin >> a[i][j];
        }
    }
    cin >> sx >> sy >> ex >> ey;
    sx--,sy--,ex--,ey--;
    memset(ans,INF,sizeof ans);
    bfs();
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}